package com.snail.cloud.controller;

import com.snail.cloud.entities.PayDTO;
import com.snail.cloud.resp.ResultData;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 订单控制器，订单的增删改查
 *
 * @author snail
 * @date 2024/9/24
 */
@Slf4j
@RestController
@RequestMapping("/consumer")
public class OrderController {

    @Resource
    private DiscoveryClient discoveryClient;

    // 服务注册中心上的微服务名称
    public static final String URL = "http://cloud-payment-service";

    @Resource
    private RestTemplate restTemplate;

    /**
     * 添加订单
     *
     * @param payDTO 支付dto
     * @return {@link ResultData }
     * @author Snail
     * @date 2024/09/24
     */
    @PostMapping("/pay/add")
    public ResultData addOrder(@RequestBody PayDTO payDTO) {
        return restTemplate.postForObject(URL + "/pay/add", payDTO, ResultData.class);
    }

    /**
     * 删除订单
     *
     * @param id ID
     * @return {@link ResultData }
     * @author Snail
     * @date 2024/09/24
     */
    @GetMapping("/pay/delete/{id}")
    public ResultData deleteOrder(@PathVariable("id") Integer id) {
       restTemplate.delete(URL + "/pay/delete/" + id);
       return ResultData.success("删除成功");
    }

    /**
     * 更新订单
     *
     * @param payDTO 支付dto
     * @return {@link ResultData }
     * @author Snail
     * @date 2024/09/24
     */
    @PutMapping("/pay/update")
    public ResultData updateOrder(@RequestBody PayDTO payDTO) {
        ResponseEntity<ResultData> exchange = restTemplate.exchange(URL + "/pay/update", HttpMethod.PUT, new HttpEntity<>(payDTO), ResultData.class, payDTO);
        return exchange.getBody();
        // return ResultData.success(exchange);


    }


    /**
     * 获取订单信息
     *
     * @param id ID
     * @return {@link ResultData }
     * @author Snail
     * @date 2024/09/24
     */
    @GetMapping("/pay/get/{id}")
    public ResultData getOrder(@PathVariable("id") Integer id) {
        return restTemplate.getForObject(URL + "/pay/get/" + id, ResultData.class, id);
    }

    /**
     * 获得所有订单信息
     *
     * @return {@link ResultData }
     * @author Snail
     * @date 2024/09/24
     */
    @GetMapping("/pay/getAll")
    public ResultData getAllOrder() {
        return restTemplate.getForObject(URL + "/pay/list", ResultData.class);
    }


    /**
     * 通过consul获取配置信息
     *
     * @return {@link String }
     * @author Snail
     * @date 2024/09/25
     */
    @GetMapping("/info")
    public String getInfoByConsul() {
       return restTemplate.getForObject(URL + "/pay/info", String.class);

    }

    /**
     * 通过discovery获取服务信息
     *
     * @return {@link String }
     * @author Snail
     * @date 2024/09/25
     */
    @GetMapping("/discovery")
    public String discovery() {
        List<String> services = discoveryClient.getServices();
        for (String element : services) {
            System.out.println(element);
        }
        System.out.println("=================");
        List<ServiceInstance> instances = discoveryClient.getInstances("cloud-payment-service");
        for (ServiceInstance element : instances) {
            System.out.println(element.getInstanceId()+"\t"+element.getHost()+"\t"+element.getPort()+"\t"+element.getUri());
        }
        return instances.get(0).getInstanceId()+""+instances.get(0).getPort();
    }
}
